import 'package:fish_redux/fish_redux.dart';

import 'action.dart';
import 'state.dart';

Reducer<PracticeVoiceState> buildReducer() {
  return asReducer(
    <Object, Reducer<PracticeVoiceState>>{
      PracticeVoiceAction.token: _token,
      PracticeVoiceAction.doneVoiceAi: _doneVoiceAi,
      PracticeVoiceAction.selectGrade: _selectGrade,
      PracticeVoiceAction.selectType: _selectType,
      PracticeVoiceAction.focus: _focus,
    },
  );
}

PracticeVoiceState _token(PracticeVoiceState state, Action action) {
  final PracticeVoiceState newState = state.clone()..aliToken = action.payload;
  return newState;
}

PracticeVoiceState _selectGrade(PracticeVoiceState state, Action action) {
  return state.clone()..gradeValue = action.payload;
}

PracticeVoiceState _selectType(PracticeVoiceState state, Action action) {
  return state.clone()..typeValue = action.payload;
}

PracticeVoiceState _focus(PracticeVoiceState state, Action action) {
  return state.clone()..isTitle = action.payload;
}

PracticeVoiceState _doneVoiceAi(PracticeVoiceState state, Action action) {
  var newStr = action.payload;
  newStr = newStr.replaceAll('嗯', '');
  final PracticeVoiceState newState = state.clone();

  if (newState.isTitle) {
    newState.titleEditController.text += newStr;
  } else {
    newState.contentEditController.text += newStr;
  }

  return newState;
}
